home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: Splitting String ?
- Date: 13 Jan 1996 19:12:18 GMT
- Organization: Internet Access Group, Orlando, Florida
- Distribution: world
- Message-ID: <4d906i$pq2@news.iag.net>
- References: <HAKOLA.96Jan12151128@jung.hut.fi> <4d67an$303@unda.fi>
- NNTP-Posting-Host: pm2-orl3.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4d67an$303@unda.fi>, olle@gustav.unda.fi says...
- ~
- ~In article <HAKOLA.96Jan12151128@jung.hut.fi>, hakola@cadmail.hut.fi (Petri
- Hakola) writes:
- ~ ~
- ~ ~ Have I missed something (again:)
- ~
- ~Yes...
- ~
- ~ ~ or why doesn't this code
- ~ ~ work? I should split dos-a-like-filename and add new postfix
- ~ ~ instead of old one (i.e. DATA.TXT --> DATA.UPD It seems to
- ~ ~ work correctly if the filename has an old postfix, but if
- ~ ~ there isn't one start won't return what it should.
- ~ ~
- <snip>
- ~ ~ main() {
- ~ ~ printf("%s\n",newname("LONGNAME.TXT"));
- ~ ~ printf("%s\n",newname("NOEND"));
- ~ ~ }
- ~
- ~(In the prevous code the variable start is not really needed, but wait...)
- ~
- ~You'll need to e.g. malloc() in newname for a string with enough room for the
- ~original string (w/o the extension) plus end (plus '\0') and copy the input
- ~string / head of the input string there and _then_ strcat on that!
- ~
- ~(You cannot simply strcat on "NOEND" and I don't think that it is a great
- idea
- ~to modify "LONGNAME.TXT", either, even if it just happens to work in your
- ~environment, and even if this is just an example of using newname!)
-
- Right, he can't safely modify either one. String literals can legally be
- stored as read only. So, the ability to modify them is implementation
- defined. It is always safest to treat them as const.
-
- His attempt to add an extension to "NOEND" is undoubtedly what caused the
- failure, since it would have overwritten whatever happened to follow it.
-
-
- A safe version might go something like:
-
- terms:
- FileNameLen = max length for filename with no extension or dot.
- ExtLen = max length of extension with a dot.
-
- 1. Define function to accept a const char * and return a char * (or const).
-
- 2. Define: char *dot; char *ext = ".txt" /* your extension string */
-
- 3. Define a char array (ary) of FileNameLen + ExtLen + 1 (for '\0'). Either
- A. malloc it (the caller will need to free memory later).
- B. define a (local or global) static (the contents will be overwritten
- on each call).
-
- 4. Use strrchr to set dot to the last '.' on the source string.
-
- 5. Protect the bounds of ary:
- If dot == NULL, test strlen of source against FileNameLen
- else test (dot - source) against FileNameLen
- A. Do not procede without handling any error.
-
- 6. copy the source to your ary.
-
- 7. if dot == NULL, use strcat to append your extension to ary
- else use strcpy to append it to ary at ary + (dot - source)
-
- 8. return ary
-
- Did I miss anything?
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-